<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"><title>React Router 6 快速上手 | Jushden的个人博客</title><meta name="author" content="Jushden"><meta name="copyright" content="Jushden"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="React Router 6 快速上手1.概述 React Router 以三个不同的包发布到 npm 上，它们分别为：  react-router: 路由的核心库，提供了很多的：组件、钩子。 react-router-dom: 包含react-router所有内容，并添加一些专门用于 DOM 的组件，例如 &lt;BrowserRouter&gt;等 。 react-router-native:">
<meta property="og:type" content="article">
<meta property="og:title" content="React Router 6 快速上手">
<meta property="og:url" content="https://jushden.gitee.io/2023/07/29/ReactRouter6%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B/index.html">
<meta property="og:site_name" content="Jushden的个人博客">
<meta property="og:description" content="React Router 6 快速上手1.概述 React Router 以三个不同的包发布到 npm 上，它们分别为：  react-router: 路由的核心库，提供了很多的：组件、钩子。 react-router-dom: 包含react-router所有内容，并添加一些专门用于 DOM 的组件，例如 &lt;BrowserRouter&gt;等 。 react-router-native:">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img1.baidu.com/it/u=3255567619,1492410640&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500">
<meta property="article:published_time" content="2023-07-29T09:15:15.871Z">
<meta property="article:modified_time" content="2023-07-29T09:17:54.470Z">
<meta property="article:author" content="Jushden">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img1.baidu.com/it/u=3255567619,1492410640&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500"><link rel="shortcut icon" href="/zhuxiaodong.gitee.io/img/favicon.png"><link rel="canonical" href="https://jushden.gitee.io/2023/07/29/ReactRouter6%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/zhuxiaodong.gitee.io/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/zhuxiaodong.gitee.io/',
  algolia: undefined,
  localSearch: {"path":"/zhuxiaodong.gitee.io/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: true,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'React Router 6 快速上手',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-07-29 17:17:54'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/zhuxiaodong.gitee.io/img/duola.jpeg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/zhuxiaodong.gitee.io/archives/"><div class="headline">文章</div><div class="length-num">12</div></a><a href="/zhuxiaodong.gitee.io/tags/"><div class="headline">标签</div><div class="length-num">0</div></a><a href="/zhuxiaodong.gitee.io/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/zhuxiaodong.gitee.io/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-graduation-cap"></i><span> 博文</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/zhuxiaodong.gitee.io/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></li><li><a class="site-page child" href="/zhuxiaodong.gitee.io/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/zhuxiaodong.gitee.io/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 生活</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/zhuxiaodong.gitee.io/photos/"><i class="fa-fw fa fa-camera-retro"></i><span> 相册</span></a></li><li><a class="site-page child" href="/zhuxiaodong.gitee.io/music/"><i class="fa-fw fa fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/zhuxiaodong.gitee.io/movies/"><i class="fa-fw fas fa-video"></i><span> 影视</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/zhuxiaodong.gitee.io/links/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/zhuxiaodong.gitee.io/comment/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/zhuxiaodong.gitee.io/about/"><i class="fa-fw fas fa-heart"></i><span> 关于笔者</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://img1.baidu.com/it/u=3255567619,1492410640&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=889&amp;h=500')"><nav id="nav"><span id="blog-info"><a href="/zhuxiaodong.gitee.io/" title="Jushden的个人博客"><span class="site-name">Jushden的个人博客</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/zhuxiaodong.gitee.io/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-graduation-cap"></i><span> 博文</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/zhuxiaodong.gitee.io/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></li><li><a class="site-page child" href="/zhuxiaodong.gitee.io/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/zhuxiaodong.gitee.io/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 生活</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/zhuxiaodong.gitee.io/photos/"><i class="fa-fw fa fa-camera-retro"></i><span> 相册</span></a></li><li><a class="site-page child" href="/zhuxiaodong.gitee.io/music/"><i class="fa-fw fa fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/zhuxiaodong.gitee.io/movies/"><i class="fa-fw fas fa-video"></i><span> 影视</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/zhuxiaodong.gitee.io/links/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/zhuxiaodong.gitee.io/comment/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/zhuxiaodong.gitee.io/about/"><i class="fa-fw fas fa-heart"></i><span> 关于笔者</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">React Router 6 快速上手</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-07-29T09:15:15.871Z" title="发表于 2023-07-29 17:15:15">2023-07-29</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-07-29T09:17:54.470Z" title="更新于 2023-07-29 17:17:54">2023-07-29</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="React Router 6 快速上手"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id=""><a href="#" class="headerlink" title=""></a><img src="https://ftp.bmp.ovh/imgs/2022/02/f86f98dd6ff7d01b.png" style="width:50%;margin-left:50%;transform:translateX(-50%)" /></h1><h1 id="React-Router-6-快速上手"><a href="#React-Router-6-快速上手" class="headerlink" title="React Router 6 快速上手"></a>React Router 6 快速上手</h1><h2 id="1-概述"><a href="#1-概述" class="headerlink" title="1.概述"></a>1.概述</h2><ol>
<li><p>React Router 以三个不同的包发布到 npm 上，它们分别为：</p>
<ol>
<li>react-router: 路由的核心库，提供了很多的：组件、钩子。</li>
<li><strong style="color:#dd4d40"><strong>react-router-dom:</strong></strong > <strong style="color:#dd4d40">包含react-router所有内容，并添加一些专门用于 DOM 的组件，例如 <code>&lt;BrowserRouter&gt;</code>等 </strong>。</li>
<li>react-router-native: 包括react-router所有内容，并添加一些专门用于ReactNative的API，例如:<code>&lt;NativeRouter&gt;</code>等。</li>
</ol>
</li>
<li><p>与React Router 5.x 版本相比，改变了什么？</p>
<ol>
<li><p>内置组件的变化：移除<code>&lt;Switch/&gt;</code> ，新增 <code>&lt;Routes/&gt;</code>等。</p>
</li>
<li><p>语法的变化：<code>component=&#123;About&#125;</code> 变为 <code>element=&#123;&lt;About/&gt;&#125;</code>等。</p>
</li>
<li><p>新增多个hook：<code>useParams</code>、<code>useNavigate</code>、<code>useMatch</code>等。</p>
</li>
<li><p><strong style="color:#dd4d40">官方明确推荐函数式组件了！！！</strong></p>
<p>……</p>
</li>
</ol>
</li>
</ol>
<h2 id="2-Component"><a href="#2-Component" class="headerlink" title="2.Component"></a>2.Component</h2><h3 id="1-lt-BrowserRouter-gt"><a href="#1-lt-BrowserRouter-gt" class="headerlink" title="1. &lt;BrowserRouter&gt;"></a>1. <code>&lt;BrowserRouter&gt;</code></h3><ol>
<li><p>说明：<code>&lt;BrowserRouter&gt; </code>用于包裹整个应用。</p>
</li>
<li><p>示例代码：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">React</span> <span class="keyword">from</span> <span class="string">&quot;react&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="title class_">ReactDOM</span> <span class="keyword">from</span> <span class="string">&quot;react-dom&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">BrowserRouter</span> &#125; <span class="keyword">from</span> <span class="string">&quot;react-router-dom&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="title class_">ReactDOM</span>.<span class="title function_">render</span>(</span><br><span class="line">  <span class="language-xml"><span class="tag">&lt;<span class="name">BrowserRouter</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    &#123;/* 整体结构（通常为App组件） */&#125;</span></span><br><span class="line"><span class="language-xml">  <span class="tag">&lt;/<span class="name">BrowserRouter</span>&gt;</span></span>,root</span><br><span class="line">);</span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="2-lt-HashRouter-gt"><a href="#2-lt-HashRouter-gt" class="headerlink" title="2. &lt;HashRouter&gt;"></a>2. <code>&lt;HashRouter&gt;</code></h3><ol>
<li>说明：作用与<code>&lt;BrowserRouter&gt;</code>一样，但<code>&lt;HashRouter&gt;</code>修改的是地址栏的hash值。</li>
<li>备注：6.x版本中<code>&lt;HashRouter&gt;</code>、<code>&lt;BrowserRouter&gt; </code> 的用法与 5.x 相同。</li>
</ol>
<h3 id="3-lt-Routes-gt-与-lt-Route-gt"><a href="#3-lt-Routes-gt-与-lt-Route-gt" class="headerlink" title="3. &lt;Routes/&gt; 与 &lt;Route/&gt;"></a>3. <code>&lt;Routes/&gt; 与 &lt;Route/&gt;</code></h3><ol>
<li><p>v6版本中移出了先前的<code>&lt;Switch&gt;</code>，引入了新的替代者：<code>&lt;Routes&gt;</code>。</p>
</li>
<li><p><code>&lt;Routes&gt;</code> 和 <code>&lt;Route&gt;</code>要配合使用，且必须要用<code>&lt;Routes&gt;</code>包裹<code>&lt;Route&gt;</code>。</p>
</li>
<li><p><code>&lt;Route&gt;</code> 相当于一个 if 语句，如果其路径与当前 URL 匹配，则呈现其对应的组件。</p>
</li>
<li><p><code>&lt;Route caseSensitive&gt;</code> 属性用于指定：匹配时是否区分大小写（默认为 false）。</p>
</li>
<li><p>当URL发生变化时，<code>&lt;Routes&gt; </code>都会查看其所有子<code> &lt;Route&gt;</code> 元素以找到最佳匹配并呈现组件 。</p>
</li>
<li><p><code>&lt;Route&gt;</code> 也可以嵌套使用，且可配合<code>useRoutes()</code>配置 “路由表” ，但需要通过 <code>&lt;Outlet&gt;</code> 组件来渲染其子路由。</p>
</li>
<li><p>示例代码：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">&lt;<span class="title class_">Routes</span>&gt;</span><br><span class="line">    <span class="comment">/*path属性用于定义路径，element属性用于定义当前路径所对应的组件*/</span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">Route</span> <span class="attr">path</span>=<span class="string">&quot;/login&quot;</span> <span class="attr">element</span>=<span class="string">&#123;</span>&lt;<span class="attr">Login</span> /&gt;</span>&#125;&gt;<span class="tag">&lt;/<span class="name">Route</span>&gt;</span></span></span><br><span class="line"></span><br><span class="line">		<span class="comment">/*用于定义嵌套路由，home是一级路由，对应的路径/home*/</span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">Route</span> <span class="attr">path</span>=<span class="string">&quot;home&quot;</span> <span class="attr">element</span>=<span class="string">&#123;</span>&lt;<span class="attr">Home</span> /&gt;</span>&#125;&gt;</span></span><br><span class="line"><span class="language-xml">       /*test1 和 test2 是二级路由,对应的路径是/home/test1 或 /home/test2*/</span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">Route</span> <span class="attr">path</span>=<span class="string">&quot;test1&quot;</span> <span class="attr">element</span>=<span class="string">&#123;</span>&lt;<span class="attr">Test</span>/&gt;</span>&#125;&gt;<span class="tag">&lt;/<span class="name">Route</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">Route</span> <span class="attr">path</span>=<span class="string">&quot;test2&quot;</span> <span class="attr">element</span>=<span class="string">&#123;</span>&lt;<span class="attr">Test2</span>/&gt;</span>&#125;&gt;<span class="tag">&lt;/<span class="name">Route</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">		<span class="tag">&lt;/<span class="name">Route</span>&gt;</span></span></span><br><span class="line">	</span><br><span class="line">		<span class="comment">//Route也可以不写element属性, 这时就是用于展示嵌套的路由 .所对应的路径是/users/xxx</span></span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">Route</span> <span class="attr">path</span>=<span class="string">&quot;users&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">       <span class="tag">&lt;<span class="name">Route</span> <span class="attr">path</span>=<span class="string">&quot;xxx&quot;</span> <span class="attr">element</span>=<span class="string">&#123;</span>&lt;<span class="attr">Demo</span> /&gt;</span>&#125; /&gt;</span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">Route</span>&gt;</span></span></span><br><span class="line">&lt;/<span class="title class_">Routes</span>&gt;</span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="4-lt-Link-gt"><a href="#4-lt-Link-gt" class="headerlink" title="4. &lt;Link&gt;"></a>4. <code>&lt;Link&gt;</code></h3><ol>
<li><p>作用: 修改URL，且不发送网络请求（路由链接）。</p>
</li>
<li><p>注意: 外侧需要用<code>&lt;BrowserRouter&gt;</code>或<code>&lt;HashRouter&gt;</code>包裹。</p>
</li>
<li><p>示例代码：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">Link</span> &#125; <span class="keyword">from</span> <span class="string">&quot;react-router-dom&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Test</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    	<span class="tag">&lt;<span class="name">Link</span> <span class="attr">to</span>=<span class="string">&quot;/路径&quot;</span>&gt;</span>按钮<span class="tag">&lt;/<span class="name">Link</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">  );</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="5-lt-NavLink-gt"><a href="#5-lt-NavLink-gt" class="headerlink" title="5. &lt;NavLink&gt;"></a>5. <code>&lt;NavLink&gt;</code></h3><ol>
<li><p>作用: 与<code>&lt;Link&gt;</code>组件类似，且可实现导航的“高亮”效果。</p>
</li>
<li><p>示例代码：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 注意: NavLink默认类名是active，下面是指定自定义的class</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//自定义样式</span></span><br><span class="line">&lt;<span class="title class_">NavLink</span></span><br><span class="line">    to=<span class="string">&quot;login&quot;</span></span><br><span class="line">    className=&#123;<span class="function">(<span class="params">&#123; isActive &#125;</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;home&#x27;</span>, isActive)</span><br><span class="line">        <span class="keyword">return</span> isActive ? <span class="string">&#x27;base one&#x27;</span> : <span class="string">&#x27;base&#x27;</span></span><br><span class="line">    &#125;&#125;</span><br><span class="line">&gt;login&lt;/<span class="title class_">NavLink</span>&gt;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">	默认情况下，当Home的子组件匹配成功，Home的导航也会高亮，</span></span><br><span class="line"><span class="comment">	当NavLink上添加了end属性后，若Home的子组件匹配成功，则Home的导航没有高亮效果。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">NavLink</span> <span class="attr">to</span>=<span class="string">&quot;home&quot;</span> <span class="attr">end</span> &gt;</span>home<span class="tag">&lt;/<span class="name">NavLink</span>&gt;</span></span></span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="6-lt-Navigate-gt"><a href="#6-lt-Navigate-gt" class="headerlink" title="6. &lt;Navigate&gt;"></a>6. <code>&lt;Navigate&gt;</code></h3><ol>
<li><p>作用：只要<code>&lt;Navigate&gt;</code>组件被渲染，就会修改路径，切换视图。</p>
</li>
<li><p><code>replace</code>属性用于控制跳转模式（push 或 replace，默认是push）。</p>
</li>
<li><p>示例代码：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">React</span>,&#123;useState&#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;<span class="title class_">Navigate</span>&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">function</span> <span class="title function_">Home</span>(<span class="params"></span>) &#123;</span><br><span class="line">	<span class="keyword">const</span> [sum,setSum] = <span class="title function_">useState</span>(<span class="number">1</span>)</span><br><span class="line">	<span class="keyword">return</span> (</span><br><span class="line">		<span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">			<span class="tag">&lt;<span class="name">h3</span>&gt;</span>我是Home的内容<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">			&#123;/* 根据sum的值决定是否切换视图 */&#125;</span></span><br><span class="line"><span class="language-xml">			&#123;sum === 1 ? <span class="tag">&lt;<span class="name">h4</span>&gt;</span>sum的值为&#123;sum&#125;<span class="tag">&lt;/<span class="name">h4</span>&gt;</span> : <span class="tag">&lt;<span class="name">Navigate</span> <span class="attr">to</span>=<span class="string">&quot;/about&quot;</span> <span class="attr">replace</span>=<span class="string">&#123;true&#125;/</span>&gt;</span>&#125;</span></span><br><span class="line"><span class="language-xml">			<span class="tag">&lt;<span class="name">button</span> <span class="attr">onClick</span>=<span class="string">&#123;()</span>=&gt;</span>setSum(2)&#125;&gt;点我将sum变为2<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">		<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">	)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="7-lt-Outlet-gt"><a href="#7-lt-Outlet-gt" class="headerlink" title="7. &lt;Outlet&gt;"></a>7. <code>&lt;Outlet&gt;</code></h3><ol>
<li><p>当<code>&lt;Route&gt;</code>产生嵌套时，渲染其对应的后续子路由。</p>
</li>
<li><p>示例代码：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//根据路由表生成对应的路由规则</span></span><br><span class="line"><span class="keyword">const</span> element = <span class="title function_">useRoutes</span>([</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="attr">path</span>:<span class="string">&#x27;/about&#x27;</span>,</span><br><span class="line">    <span class="attr">element</span>:<span class="language-xml"><span class="tag">&lt;<span class="name">About</span>/&gt;</span></span></span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="attr">path</span>:<span class="string">&#x27;/home&#x27;</span>,</span><br><span class="line">    <span class="attr">element</span>:<span class="language-xml"><span class="tag">&lt;<span class="name">Home</span>/&gt;</span></span>,</span><br><span class="line">    <span class="attr">children</span>:[</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="attr">path</span>:<span class="string">&#x27;news&#x27;</span>,</span><br><span class="line">        <span class="attr">element</span>:<span class="language-xml"><span class="tag">&lt;<span class="name">News</span>/&gt;</span></span></span><br><span class="line">      &#125;,</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="attr">path</span>:<span class="string">&#x27;message&#x27;</span>,</span><br><span class="line">        <span class="attr">element</span>:<span class="language-xml"><span class="tag">&lt;<span class="name">Message</span>/&gt;</span></span>,</span><br><span class="line">      &#125;</span><br><span class="line">    ]</span><br><span class="line">  &#125;</span><br><span class="line">])</span><br><span class="line"></span><br><span class="line"><span class="comment">//Home.js</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">React</span> <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;<span class="title class_">NavLink</span>,<span class="title class_">Outlet</span>&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">function</span> <span class="title function_">Home</span>(<span class="params"></span>) &#123;</span><br><span class="line">	<span class="keyword">return</span> (</span><br><span class="line">		<span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">			<span class="tag">&lt;<span class="name">h2</span>&gt;</span>Home组件内容<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">			<span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">				<span class="tag">&lt;<span class="name">ul</span> <span class="attr">className</span>=<span class="string">&quot;nav nav-tabs&quot;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">					<span class="tag">&lt;<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">						<span class="tag">&lt;<span class="name">NavLink</span> <span class="attr">className</span>=<span class="string">&quot;list-group-item&quot;</span> <span class="attr">to</span>=<span class="string">&quot;news&quot;</span>&gt;</span>News<span class="tag">&lt;/<span class="name">NavLink</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">					<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">					<span class="tag">&lt;<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">						<span class="tag">&lt;<span class="name">NavLink</span> <span class="attr">className</span>=<span class="string">&quot;list-group-item&quot;</span> <span class="attr">to</span>=<span class="string">&quot;message&quot;</span>&gt;</span>Message<span class="tag">&lt;/<span class="name">NavLink</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">					<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">				<span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">				&#123;/* 指定路由组件呈现的位置 */&#125;</span></span><br><span class="line"><span class="language-xml">				<span class="tag">&lt;<span class="name">Outlet</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml">			<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">		<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">	)</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ol>
<h2 id="3-Hooks"><a href="#3-Hooks" class="headerlink" title="3.Hooks"></a>3.Hooks</h2><h3 id="1-useRoutes"><a href="#1-useRoutes" class="headerlink" title="1. useRoutes()"></a>1. useRoutes()</h3><ol>
<li><p>作用：根据路由表，动态创建<code>&lt;Routes&gt;</code>和<code>&lt;Route&gt;</code>。</p>
</li>
<li><p>示例代码：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//路由表配置：src/routes/index.js</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">About</span> <span class="keyword">from</span> <span class="string">&#x27;../pages/About&#x27;</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Home</span> <span class="keyword">from</span> <span class="string">&#x27;../pages/Home&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;<span class="title class_">Navigate</span>&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> [</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="attr">path</span>:<span class="string">&#x27;/about&#x27;</span>,</span><br><span class="line">		<span class="attr">element</span>:<span class="language-xml"><span class="tag">&lt;<span class="name">About</span>/&gt;</span></span></span><br><span class="line">	&#125;,</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="attr">path</span>:<span class="string">&#x27;/home&#x27;</span>,</span><br><span class="line">		<span class="attr">element</span>:<span class="language-xml"><span class="tag">&lt;<span class="name">Home</span>/&gt;</span></span></span><br><span class="line">	&#125;,</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="attr">path</span>:<span class="string">&#x27;/&#x27;</span>,</span><br><span class="line">		<span class="attr">element</span>:<span class="language-xml"><span class="tag">&lt;<span class="name">Navigate</span> <span class="attr">to</span>=<span class="string">&quot;/about&quot;</span>/&gt;</span></span></span><br><span class="line">	&#125;</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line"><span class="comment">//App.jsx</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">React</span> <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;<span class="title class_">NavLink</span>,useRoutes&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="keyword">import</span> routes <span class="keyword">from</span> <span class="string">&#x27;./routes&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">function</span> <span class="title function_">App</span>(<span class="params"></span>) &#123;</span><br><span class="line">	<span class="comment">//根据路由表生成对应的路由规则</span></span><br><span class="line">	<span class="keyword">const</span> element = <span class="title function_">useRoutes</span>(routes)</span><br><span class="line">	<span class="keyword">return</span> (</span><br><span class="line">		<span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">			......</span></span><br><span class="line"><span class="language-xml">      &#123;/* 注册路由 */&#125;</span></span><br><span class="line"><span class="language-xml">      &#123;element&#125;</span></span><br><span class="line"><span class="language-xml">		  ......</span></span><br><span class="line"><span class="language-xml">		<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">	)</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="2-useNavigate"><a href="#2-useNavigate" class="headerlink" title="2. useNavigate()"></a>2. useNavigate()</h3><ol>
<li><p>作用：返回一个函数用来实现编程式导航。</p>
</li>
<li><p>示例代码：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">React</span> <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;useNavigate&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">function</span> <span class="title function_">Demo</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="keyword">const</span> navigate = <span class="title function_">useNavigate</span>()</span><br><span class="line">  <span class="keyword">const</span> <span class="title function_">handle</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="comment">//第一种使用方式：指定具体的路径</span></span><br><span class="line">    <span class="title function_">navigate</span>(<span class="string">&#x27;/login&#x27;</span>, &#123;</span><br><span class="line">      <span class="attr">replace</span>: <span class="literal">false</span>,</span><br><span class="line">      <span class="attr">state</span>: &#123;<span class="attr">a</span>:<span class="number">1</span>, <span class="attr">b</span>:<span class="number">2</span>&#125;</span><br><span class="line">    &#125;) </span><br><span class="line">    <span class="comment">//第二种使用方式：传入数值进行前进或后退，类似于5.x中的 history.go()方法</span></span><br><span class="line">    <span class="title function_">navigate</span>(-<span class="number">1</span>)</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">button</span> <span class="attr">onClick</span>=<span class="string">&#123;handle&#125;</span>&gt;</span>按钮<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">  )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="3-useParams"><a href="#3-useParams" class="headerlink" title="3. useParams()"></a>3. useParams()</h3><ol>
<li><p>作用：回当前匹配路由的<code>params</code>参数，类似于5.x中的<code>match.params</code>。</p>
</li>
<li><p>示例代码：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">React</span> <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; <span class="title class_">Routes</span>, <span class="title class_">Route</span>, useParams &#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="title class_">User</span> <span class="keyword">from</span> <span class="string">&#x27;./pages/User.jsx&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">ProfilePage</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="comment">// 获取URL中携带过来的params参数</span></span><br><span class="line">  <span class="keyword">let</span> &#123; id &#125; = <span class="title function_">useParams</span>();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">App</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">Routes</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">Route</span> <span class="attr">path</span>=<span class="string">&quot;users/:id&quot;</span> <span class="attr">element</span>=<span class="string">&#123;</span>&lt;<span class="attr">User</span> /&gt;</span>&#125;/&gt;</span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">Routes</span>&gt;</span></span></span><br><span class="line">  );</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="4-useSearchParams"><a href="#4-useSearchParams" class="headerlink" title="4. useSearchParams()"></a>4. useSearchParams()</h3><ol>
<li><p>作用：用于读取和修改当前位置的 URL 中的查询字符串。</p>
</li>
<li><p>返回一个包含两个值的数组，内容分别为：当前的seaech参数、更新search的函数。</p>
</li>
<li><p>示例代码：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">React</span> <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;useSearchParams&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">function</span> <span class="title function_">Detail</span>(<span class="params"></span>) &#123;</span><br><span class="line">	<span class="keyword">const</span> [search,setSearch] = <span class="title function_">useSearchParams</span>()</span><br><span class="line">	<span class="keyword">const</span> id = search.<span class="title function_">get</span>(<span class="string">&#x27;id&#x27;</span>)</span><br><span class="line">	<span class="keyword">const</span> title = search.<span class="title function_">get</span>(<span class="string">&#x27;title&#x27;</span>)</span><br><span class="line">	<span class="keyword">const</span> content = search.<span class="title function_">get</span>(<span class="string">&#x27;content&#x27;</span>)</span><br><span class="line">	<span class="keyword">return</span> (</span><br><span class="line">		<span class="language-xml"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">			<span class="tag">&lt;<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">				<span class="tag">&lt;<span class="name">button</span> <span class="attr">onClick</span>=<span class="string">&#123;()</span>=&gt;</span>setSearch(&#x27;id=008&amp;title=哈哈&amp;content=嘻嘻&#x27;)&#125;&gt;点我更新一下收到的search参数<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">			<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">			<span class="tag">&lt;<span class="name">li</span>&gt;</span>消息编号：&#123;id&#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">			<span class="tag">&lt;<span class="name">li</span>&gt;</span>消息标题：&#123;title&#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">			<span class="tag">&lt;<span class="name">li</span>&gt;</span>消息内容：&#123;content&#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">		<span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span></span><br><span class="line">	)</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="5-useLocation"><a href="#5-useLocation" class="headerlink" title="5. useLocation()"></a>5. useLocation()</h3><ol>
<li><p>作用：获取当前 location 信息，对标5.x中的路由组件的<code>location</code>属性。</p>
</li>
<li><p>示例代码：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">React</span> <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;useLocation&#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">function</span> <span class="title function_">Detail</span>(<span class="params"></span>) &#123;</span><br><span class="line">	<span class="keyword">const</span> x = <span class="title function_">useLocation</span>()</span><br><span class="line">	<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;@&#x27;</span>,x)</span><br><span class="line">  <span class="comment">// x就是location对象: </span></span><br><span class="line">	<span class="comment">/*</span></span><br><span class="line"><span class="comment">		&#123;</span></span><br><span class="line"><span class="comment">      hash: &quot;&quot;,</span></span><br><span class="line"><span class="comment">      key: &quot;ah9nv6sz&quot;,</span></span><br><span class="line"><span class="comment">      pathname: &quot;/login&quot;,</span></span><br><span class="line"><span class="comment">      search: &quot;?name=zs&amp;age=18&quot;,</span></span><br><span class="line"><span class="comment">      state: &#123;a: 1, b: 2&#125;</span></span><br><span class="line"><span class="comment">    &#125;</span></span><br><span class="line"><span class="comment">	*/</span></span><br><span class="line">	<span class="keyword">return</span> (</span><br><span class="line">		<span class="language-xml"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">			<span class="tag">&lt;<span class="name">li</span>&gt;</span>消息编号：&#123;id&#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">			<span class="tag">&lt;<span class="name">li</span>&gt;</span>消息标题：&#123;title&#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">			<span class="tag">&lt;<span class="name">li</span>&gt;</span>消息内容：&#123;content&#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">		<span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span></span><br><span class="line">	)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">  </span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="6-useMatch"><a href="#6-useMatch" class="headerlink" title="6. useMatch()"></a>6. useMatch()</h3><ol>
<li><p>作用：返回当前匹配信息，对标5.x中的路由组件的<code>match</code>属性。</p>
</li>
<li><p>示例代码：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">&lt;<span class="title class_">Route</span> path=<span class="string">&quot;/login/:page/:pageSize&quot;</span> element=&#123;<span class="language-xml"><span class="tag">&lt;<span class="name">Login</span> /&gt;</span></span>&#125;/&gt;</span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">NavLink</span> <span class="attr">to</span>=<span class="string">&quot;/login/1/10&quot;</span>&gt;</span>登录<span class="tag">&lt;/<span class="name">NavLink</span>&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">function</span> <span class="title function_">Login</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="keyword">const</span> match = <span class="title function_">useMatch</span>(<span class="string">&#x27;/login/:x/:y&#x27;</span>)</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(match) <span class="comment">//输出match对象</span></span><br><span class="line">  <span class="comment">//match对象内容如下：</span></span><br><span class="line">  <span class="comment">/*</span></span><br><span class="line"><span class="comment">  	&#123;</span></span><br><span class="line"><span class="comment">      params: &#123;x: &#x27;1&#x27;, y: &#x27;10&#x27;&#125;</span></span><br><span class="line"><span class="comment">      pathname: &quot;/LoGin/1/10&quot;  </span></span><br><span class="line"><span class="comment">      pathnameBase: &quot;/LoGin/1/10&quot;</span></span><br><span class="line"><span class="comment">      pattern: &#123;</span></span><br><span class="line"><span class="comment">      	path: &#x27;/login/:x/:y&#x27;, </span></span><br><span class="line"><span class="comment">      	caseSensitive: false, </span></span><br><span class="line"><span class="comment">      	end: false</span></span><br><span class="line"><span class="comment">      &#125;</span></span><br><span class="line"><span class="comment">    &#125;</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">  	<span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Login<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">  )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="7-useInRouterContext"><a href="#7-useInRouterContext" class="headerlink" title="7. useInRouterContext()"></a>7. useInRouterContext()</h3><p>​			作用：如果组件在 <code>&lt;Router&gt;</code> 的上下文中呈现，则 <code>useInRouterContext</code> 钩子返回 true，否则返回 false。</p>
<h3 id="8-useNavigationType"><a href="#8-useNavigationType" class="headerlink" title="8. useNavigationType()"></a>8. useNavigationType()</h3><ol>
<li>作用：返回当前的导航类型（用户是如何来到当前页面的）。</li>
<li>返回值：<code>POP</code>、<code>PUSH</code>、<code>REPLACE</code>。</li>
<li>备注：<code>POP</code>是指在浏览器中直接打开了这个路由组件（刷新页面）。</li>
</ol>
<h3 id="9-useOutlet"><a href="#9-useOutlet" class="headerlink" title="9. useOutlet()"></a>9. useOutlet()</h3><ol>
<li><p>作用：用来呈现当前组件中渲染的嵌套路由。</p>
</li>
<li><p>示例代码：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> result = <span class="title function_">useOutlet</span>()</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result)</span><br><span class="line"><span class="comment">// 如果嵌套路由没有挂载,则result为null</span></span><br><span class="line"><span class="comment">// 如果嵌套路由已经挂载,则展示嵌套的路由对象</span></span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="10-useResolvedPath"><a href="#10-useResolvedPath" class="headerlink" title="10.useResolvedPath()"></a>10.useResolvedPath()</h3><ol>
<li>作用：给定一个 URL值，解析其中的：path、search、hash值。</li>
</ol>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://jushden.gitee.io">Jushden</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://jushden.gitee.io/2023/07/29/ReactRouter6%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B/">https://jushden.gitee.io/2023/07/29/ReactRouter6%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://jushden.gitee.io" target="_blank">Jushden的个人博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://img1.baidu.com/it/u=3255567619,1492410640&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=889&amp;h=500" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/zhuxiaodong.gitee.io/2023/07/29/React%E6%89%A9%E5%B1%95/" title="React扩展"><img class="cover" src="https://img1.baidu.com/it/u=3255567619,1492410640&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=889&amp;h=500" onerror="onerror=null;src='/zhuxiaodong.gitee.io/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">React扩展</div></div></a></div><div class="next-post pull-right"><a href="/zhuxiaodong.gitee.io/2023/07/29/Git%E6%8F%90%E4%BA%A4%E4%BB%A3%E7%A0%81%E5%88%B0%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93/" title="Git提交代码到远程仓库"><img class="cover" src="https://img1.baidu.com/it/u=3255567619,1492410640&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=889&amp;h=500" onerror="onerror=null;src='/zhuxiaodong.gitee.io/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Git提交代码到远程仓库</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/zhuxiaodong.gitee.io/img/duola.jpeg" onerror="this.onerror=null;this.src='/zhuxiaodong.gitee.io/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Jushden</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/zhuxiaodong.gitee.io/archives/"><div class="headline">文章</div><div class="length-num">12</div></a><a href="/zhuxiaodong.gitee.io/tags/"><div class="headline">标签</div><div class="length-num">0</div></a><a href="/zhuxiaodong.gitee.io/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/Jushden"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/Jushden" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:1416201335@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">欢迎来到朱晓东的博客网站</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link"><span class="toc-number">1.</span> <span class="toc-text"></span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#React-Router-6-%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B"><span class="toc-number">2.</span> <span class="toc-text">React Router 6 快速上手</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E6%A6%82%E8%BF%B0"><span class="toc-number">2.1.</span> <span class="toc-text">1.概述</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-Component"><span class="toc-number">2.2.</span> <span class="toc-text">2.Component</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-lt-BrowserRouter-gt"><span class="toc-number">2.2.1.</span> <span class="toc-text">1. &lt;BrowserRouter&gt;</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-lt-HashRouter-gt"><span class="toc-number">2.2.2.</span> <span class="toc-text">2. &lt;HashRouter&gt;</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-lt-Routes-gt-%E4%B8%8E-lt-Route-gt"><span class="toc-number">2.2.3.</span> <span class="toc-text">3. &lt;Routes&#x2F;&gt; 与 &lt;Route&#x2F;&gt;</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-lt-Link-gt"><span class="toc-number">2.2.4.</span> <span class="toc-text">4. &lt;Link&gt;</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-lt-NavLink-gt"><span class="toc-number">2.2.5.</span> <span class="toc-text">5. &lt;NavLink&gt;</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-lt-Navigate-gt"><span class="toc-number">2.2.6.</span> <span class="toc-text">6. &lt;Navigate&gt;</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-lt-Outlet-gt"><span class="toc-number">2.2.7.</span> <span class="toc-text">7. &lt;Outlet&gt;</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-Hooks"><span class="toc-number">2.3.</span> <span class="toc-text">3.Hooks</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-useRoutes"><span class="toc-number">2.3.1.</span> <span class="toc-text">1. useRoutes()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-useNavigate"><span class="toc-number">2.3.2.</span> <span class="toc-text">2. useNavigate()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-useParams"><span class="toc-number">2.3.3.</span> <span class="toc-text">3. useParams()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-useSearchParams"><span class="toc-number">2.3.4.</span> <span class="toc-text">4. useSearchParams()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-useLocation"><span class="toc-number">2.3.5.</span> <span class="toc-text">5. useLocation()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-useMatch"><span class="toc-number">2.3.6.</span> <span class="toc-text">6. useMatch()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-useInRouterContext"><span class="toc-number">2.3.7.</span> <span class="toc-text">7. useInRouterContext()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-useNavigationType"><span class="toc-number">2.3.8.</span> <span class="toc-text">8. useNavigationType()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#9-useOutlet"><span class="toc-number">2.3.9.</span> <span class="toc-text">9. useOutlet()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#10-useResolvedPath"><span class="toc-number">2.3.10.</span> <span class="toc-text">10.useResolvedPath()</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/zhuxiaodong.gitee.io/2023/07/29/TypeScript%E7%AC%AC%E4%B8%80%E7%AB%A0%EF%BC%9A%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8/" title="TS入门"><img src="https://img1.baidu.com/it/u=3255567619,1492410640&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=889&amp;h=500" onerror="this.onerror=null;this.src='/zhuxiaodong.gitee.io/img/404.jpg'" alt="TS入门"/></a><div class="content"><a class="title" href="/zhuxiaodong.gitee.io/2023/07/29/TypeScript%E7%AC%AC%E4%B8%80%E7%AB%A0%EF%BC%9A%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8/" title="TS入门">TS入门</a><time datetime="2023-07-29T09:15:15.878Z" title="发表于 2023-07-29 17:15:15">2023-07-29</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/zhuxiaodong.gitee.io/2023/07/29/TypeScript%E7%AC%AC%E4%BA%8C%E7%AB%A0%EF%BC%9A%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" title="TS面向对象"><img src="https://img1.baidu.com/it/u=3255567619,1492410640&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=889&amp;h=500" onerror="this.onerror=null;this.src='/zhuxiaodong.gitee.io/img/404.jpg'" alt="TS面向对象"/></a><div class="content"><a class="title" href="/zhuxiaodong.gitee.io/2023/07/29/TypeScript%E7%AC%AC%E4%BA%8C%E7%AB%A0%EF%BC%9A%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" title="TS面向对象">TS面向对象</a><time datetime="2023-07-29T09:15:15.876Z" title="发表于 2023-07-29 17:15:15">2023-07-29</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/zhuxiaodong.gitee.io/2023/07/29/React%E5%85%A8%E5%AE%B6%E6%A1%B6/" title="React全家桶"><img src="https://img1.baidu.com/it/u=3255567619,1492410640&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=889&amp;h=500" onerror="this.onerror=null;this.src='/zhuxiaodong.gitee.io/img/404.jpg'" alt="React全家桶"/></a><div class="content"><a class="title" href="/zhuxiaodong.gitee.io/2023/07/29/React%E5%85%A8%E5%AE%B6%E6%A1%B6/" title="React全家桶">React全家桶</a><time datetime="2023-07-29T09:15:15.875Z" title="发表于 2023-07-29 17:15:15">2023-07-29</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/zhuxiaodong.gitee.io/2023/07/29/React%E6%89%A9%E5%B1%95/" title="React扩展"><img src="https://img1.baidu.com/it/u=3255567619,1492410640&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=889&amp;h=500" onerror="this.onerror=null;this.src='/zhuxiaodong.gitee.io/img/404.jpg'" alt="React扩展"/></a><div class="content"><a class="title" href="/zhuxiaodong.gitee.io/2023/07/29/React%E6%89%A9%E5%B1%95/" title="React扩展">React扩展</a><time datetime="2023-07-29T09:15:15.873Z" title="发表于 2023-07-29 17:15:15">2023-07-29</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/zhuxiaodong.gitee.io/2023/07/29/ReactRouter6%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B/" title="React Router 6 快速上手"><img src="https://img1.baidu.com/it/u=3255567619,1492410640&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=889&amp;h=500" onerror="this.onerror=null;this.src='/zhuxiaodong.gitee.io/img/404.jpg'" alt="React Router 6 快速上手"/></a><div class="content"><a class="title" href="/zhuxiaodong.gitee.io/2023/07/29/ReactRouter6%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B/" title="React Router 6 快速上手">React Router 6 快速上手</a><time datetime="2023-07-29T09:15:15.871Z" title="发表于 2023-07-29 17:15:15">2023-07-29</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By Jushden</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/zhuxiaodong.gitee.io/js/utils.js"></script><script src="/zhuxiaodong.gitee.io/js/main.js"></script><script src="/zhuxiaodong.gitee.io/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><div class="js-pjax"></div><div class="aplayer no-destroy" data-id="3136952023" data-server="netease" data-type="playlist" data-fixed="true" data-autoplay="true"> </div><script defer="defer" id="fluttering_ribbon" mobile="true" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-fluttering-ribbon.min.js"></script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/click-heart.min.js" async="async" mobile="true"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/metingjs/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = ["head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.tocScrollFn && window.removeEventListener('scroll', window.tocScrollFn)
  window.scrollCollect && window.removeEventListener('scroll', scrollCollect)

  document.getElementById('rightside').style.cssText = "opacity: ''; transform: ''"
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

  typeof disqusjs === 'object' && disqusjs.destroy()
})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div><script src="/zhuxiaodong.gitee.io/js/search/local-search.js"></script></div></body></html>